/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Microsoft Corporation
 *
 * -=- Robust Distributed System Nucleus (rDSN) -=-
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#pragma once

#include <cstdint>
#include <cstdio>
#include <string>

#include "utils/api_utilities.h"
#include "utils/logging_provider.h"
#include "utils/ports.h"
#include "utils/synchronize.h"

namespace dsn {
namespace tools {

/*
 * screen_logger provides a logger which writes to terminal.
 */
class screen_logger : public logging_provider
{
public:
    explicit screen_logger(const char *, const char *);
    ~screen_logger() override = default;

    void log(const char *file,
             const char *function,
             const int line,
             log_level_t log_level,
             const char *str) override;

    virtual void flush();

private:
    void print_header(log_level_t log_level);
    void print_long_header(const char *file,
                           const char *function,
                           const int line,
                           log_level_t log_level);
    void print_body(const char *body, log_level_t log_level);

    ::dsn::utils::ex_lock_nr _lock;
    const bool _short_header;
};

/*
 * simple_logger provides a logger which writes to file.
 * The max number of lines in a logger file is 200000.
 */
class simple_logger : public logging_provider
{
public:
    // 'log_dir' is the directory to store log files, 'role_name' is the name of the process,
    // such as 'replica_server', 'meta_server' in Pegasus.
    simple_logger(const char *log_dir, const char *role_name);
    ~simple_logger() override;

    void log(const char *file,
             const char *function,
             const int line,
             log_level_t log_level,
             const char *str) override;

    void flush() override;

private:
    void print_header(log_level_t log_level);
    void print_long_header(const char *file,
                           const char *function,
                           const int line,
                           log_level_t log_level);
    void print_body(const char *body, log_level_t log_level);

    inline void add_bytes_if_valid(int bytes)
    {
        if (dsn_likely(bytes > 0)) {
            _file_bytes += static_cast<uint64_t>(bytes);
        }
    }

    void create_log_file();
    void remove_redundant_files();

private:
    ::dsn::utils::ex_lock _lock; // use recursive lock to avoid dead lock when flush() is called
                                 // in signal handler if cored for bad logging format reason.
    // The directory to store log files.
    const std::string _log_dir;
    // The path of the symlink to the latest log file.
    std::string _symlink_path;
    // The prefix of the log file names. The actual log files are prefixed by '_file_name_prefix'
    // and postfixed by timestamp.
    std::string _file_name_prefix;
    // The current log file descriptor.
    FILE *_log;
    // The byte size of the current log file.
    uint64_t _file_bytes;
};
} // namespace tools
} // namespace dsn
